﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;

namespace Com.CMS263.DAO
{
    public class JobDAO
    {
        public JobDAO() { }
        public bool Add(Job down)
        {
            try
            {
                DBHelper.TransSaveOrUpdate(down);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public Job GetJobById(int id)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Job where id=" + id).List<Job>().First();
            }
            catch
            {
                return null;
            }
        }
        public IList<Job> GetAllJob()
        {
            return null;
        }
        public bool ModifyJob(Job down)
        {
            try
            {
                DBHelper.TransUpdate(down);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteJobById(int id)
        {
            try
            {
                ISession session = DBHelper.GetISession;
                session.CreateQuery("delete from Job as di where di.id=" + id).ExecuteUpdate();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public IList<Job> GetJobByUserId(int pageSize,int currentIndex)
        {
            try
            {
                return  DBHelper.ExecuteHQL(" from Job order by addDate desc").SetFirstResult((currentIndex-1)*pageSize).SetMaxResults(pageSize).List<Job>();
                
            }
            catch
            {
                return null;
            }
        }
        
        public int GetTotal()
        {
            try
            {
                return Convert.ToInt32(DBHelper.ExecuteHQL("select count(*) from Job").UniqueResult());
            }
            catch
            {
                return 0;
            }
        }

        public IList<Job> GetJobByType()
        {
            try
            {
                return DBHelper.ExecuteHQL(" from Job order by addDate desc").List<Job>();

            }
            catch
            {
                return null;
            }
        }
    }
}
